import { defineStore } from "pinia";
import { getMusicLyric } from "../assets/axios/api";
export const useMusicStore = defineStore("music", {
  state: () => {
    return {
      playList: localStorage.getItem("music")
        ? JSON.parse(localStorage.getItem("music"))
        : [
            {
              name: "化身孤岛的鲸",
              id: 1465313631,
              pst: 0,
              t: 0,
              ar: [
                {
                  id: 1030001,
                  name: "周深",
                  tns: [],
                  alias: [],
                },
              ],
              alia: [],
              pop: 100,
              st: 0,
              rt: "",
              fee: 8,
              v: 16,
              crbt: null,
              cf: "",
              al: {
                id: 92937467,
                name: "化身孤岛的鲸",
                picUrl:
                  "https://p1.music.126.net/-qMscnByecEQ4XrBuec3sQ==/109951165165805968.jpg",
                tns: [],
                pic_str: "109951165165805968",
                pic: 109951165165805970,
              },
              dt: 265005,
              h: {
                br: 320001,
                fid: 0,
                size: 10602285,
                vd: -47129,
                sr: 48000,
              },
              m: {
                br: 192001,
                fid: 0,
                size: 6361389,
                vd: -44498,
                sr: 48000,
              },
              l: {
                br: 128001,
                fid: 0,
                size: 4240941,
                vd: -42748,
                sr: 48000,
              },
              sq: {
                br: 877859,
                fid: 0,
                size: 29079718,
                vd: -47126,
                sr: 48000,
              },
              hr: {
                br: 1644522,
                fid: 0,
                size: 54475996,
                vd: -47088,
                sr: 48000,
              },
              a: null,
              cd: "01",
              no: 1,
              rtUrl: null,
              ftype: 0,
              rtUrls: [],
              djId: 0,
              copyright: 0,
              s_id: 0,
              mark: 536870912,
              originCoverType: 0,
              originSongSimpleData: null,
              tagPicList: null,
              resourceState: true,
              version: 16,
              songJumpInfo: null,
              entertainmentTags: null,
              awardTags: null,
              single: 0,
              noCopyrightRcmd: null,
              rurl: null,
              rtype: 0,
              mst: 9,
              cp: 1416974,
              mv: 0,
              publishTime: 0,
            },
          ], //播放列表
      playIndx: localStorage.getItem("musicIdx")
        ? JSON.parse(localStorage.getItem("musicIdx"))
        : 0, //第几首
      playReady: false, //是否播放
      playPop: false, //是否显示弹出窗
      lyric: false, //歌词显示
      musicLyric: localStorage.getItem("lyric")
        ? JSON.parse(localStorage.getItem("lyric"))
        : [
            {
              minute: 0,
              second: 0,
              millisecond: 0,
              time: 0,
              lyric: " 作曲 : 徐浩",
              pre: 1000,
            },
            {
              minute: 0,
              second: 1,
              millisecond: 0,
              time: 1000,
              lyric: " 编曲 : 唐汉霄",
              pre: 2000,
            },
            {
              minute: 0,
              second: 2,
              millisecond: 0,
              time: 2000,
              lyric: " 制作人 : 唐汉霄",
              pre: 8961,
            },
            {
              minute: 0,
              second: 8,
              millisecond: 961,
              time: 8961,
              lyric: "",
              pre: 11078,
            },
            {
              minute: 0,
              second: 11,
              millisecond: 78,
              time: 11078,
              lyric: "我是只化身孤岛的蓝鲸",
              pre: 14494,
            },
            {
              minute: 0,
              second: 14,
              millisecond: 494,
              time: 14494,
              lyric: "有着最巨大的身影",
              pre: 19095,
            },
            {
              minute: 0,
              second: 19,
              millisecond: 95,
              time: 19095,
              lyric: "鱼虾在身侧穿行",
              pre: 23361,
            },
            {
              minute: 0,
              second: 23,
              millisecond: 361,
              time: 23361,
              lyric: "也有飞鸟在背上停",
              pre: 28211,
            },
            {
              minute: 0,
              second: 28,
              millisecond: 211,
              time: 28211,
              lyric: "",
              pre: 28494,
            },
            {
              minute: 0,
              second: 28,
              millisecond: 494,
              time: 28494,
              lyric: "我路过太多太美的奇景",
              pre: 31811,
            },
            {
              minute: 0,
              second: 31,
              millisecond: 811,
              time: 31811,
              lyric: "如同伊甸般的仙境",
              pre: 36078,
            },
            {
              minute: 0,
              second: 36,
              millisecond: 78,
              time: 36078,
              lyric: "而大海太平太静",
              pre: 40494,
            },
            {
              minute: 0,
              second: 40,
              millisecond: 494,
              time: 40494,
              lyric: "多少故事无人倾听",
              pre: 44512,
            },
            {
              minute: 0,
              second: 44,
              millisecond: 512,
              time: 44512,
              lyric: "",
              pre: 44861,
            },
            {
              minute: 0,
              second: 44,
              millisecond: 861,
              time: 44861,
              lyric: "我爱地中海的天晴",
              pre: 49095,
            },
            {
              minute: 0,
              second: 49,
              millisecond: 95,
              time: 49095,
              lyric: "爱西伯利亚的雪景",
              pre: 52612,
            },
            {
              minute: 0,
              second: 52,
              millisecond: 612,
              time: 52612,
              lyric: "爱万丈高空的鹰",
              pre: 54779,
            },
            {
              minute: 0,
              second: 54,
              millisecond: 779,
              time: 54779,
              lyric: "爱肚皮下的藻荇",
              pre: 57545,
            },
            {
              minute: 0,
              second: 57,
              millisecond: 545,
              time: 57545,
              lyric: "我在尽心尽力地多情",
              pre: 61095,
            },
            {
              minute: 1,
              second: 1,
              millisecond: 95,
              time: 61095,
              lyric: "直到那一天",
              pre: 65028,
            },
            {
              minute: 1,
              second: 5,
              millisecond: 28,
              time: 65028,
              lyric: "",
              pre: 65729,
            },
            {
              minute: 1,
              second: 5,
              millisecond: 729,
              time: 65729,
              lyric: "你的衣衫破旧",
              pre: 67628,
            },
            {
              minute: 1,
              second: 7,
              millisecond: 628,
              time: 67628,
              lyric: "而歌声却温柔",
              pre: 69762,
            },
            {
              minute: 1,
              second: 9,
              millisecond: 762,
              time: 69762,
              lyric: "陪我漫无目的地四处漂流",
              pre: 73895,
            },
            {
              minute: 1,
              second: 13,
              millisecond: 895,
              time: 73895,
              lyric: "我的背脊如荒丘",
              pre: 76011,
            },
            {
              minute: 1,
              second: 16,
              millisecond: 11,
              time: 76011,
              lyric: "而你却微笑摆首",
              pre: 78961,
            },
            {
              minute: 1,
              second: 18,
              millisecond: 961,
              time: 78961,
              lyric: "把它当成整个宇宙",
              pre: 82478,
            },
            {
              minute: 1,
              second: 22,
              millisecond: 478,
              time: 82478,
              lyric: "",
              pre: 82762,
            },
            {
              minute: 1,
              second: 22,
              millisecond: 762,
              time: 82762,
              lyric: "你与太阳挥手",
              pre: 84745,
            },
            {
              minute: 1,
              second: 24,
              millisecond: 745,
              time: 84745,
              lyric: "也同海鸥问候",
              pre: 86895,
            },
            {
              minute: 1,
              second: 26,
              millisecond: 895,
              time: 86895,
              lyric: "陪我爱天爱地的四处风流",
              pre: 90929,
            },
            {
              minute: 1,
              second: 30,
              millisecond: 929,
              time: 90929,
              lyric: "只是遗憾你终究",
              pre: 93294,
            },
            {
              minute: 1,
              second: 33,
              millisecond: 294,
              time: 93294,
              lyric: "无法躺在我胸口",
              pre: 96078,
            },
            {
              minute: 1,
              second: 36,
              millisecond: 78,
              time: 96078,
              lyric: "欣赏夜空最辽阔的不朽",
              pre: 99695,
            },
            {
              minute: 1,
              second: 39,
              millisecond: 695,
              time: 99695,
              lyric: "把星子放入眸",
              pre: 104328,
            },
            {
              minute: 1,
              second: 44,
              millisecond: 328,
              time: 104328,
              lyric: "",
              pre: 110313,
            },
            {
              minute: 1,
              second: 50,
              millisecond: 313,
              time: 110313,
              lyric: "我是只化身孤岛的蓝鲸",
              pre: 113029,
            },
            {
              minute: 1,
              second: 53,
              millisecond: 29,
              time: 113029,
              lyric: "有着最巨大的身影",
              pre: 117446,
            },
            {
              minute: 1,
              second: 57,
              millisecond: 446,
              time: 117446,
              lyric: "鱼虾在身侧穿行",
              pre: 121845,
            },
            {
              minute: 2,
              second: 1,
              millisecond: 845,
              time: 121845,
              lyric: "也有飞鸟在背上停",
              pre: 126861,
            },
            {
              minute: 2,
              second: 6,
              millisecond: 861,
              time: 126861,
              lyric: "",
              pre: 127163,
            },
            {
              minute: 2,
              second: 7,
              millisecond: 163,
              time: 127163,
              lyric: "我有着太冷太清的天性",
              pre: 130495,
            },
            {
              minute: 2,
              second: 10,
              millisecond: 495,
              time: 130495,
              lyric: "对天上的她动过情",
              pre: 134461,
            },
            {
              minute: 2,
              second: 14,
              millisecond: 461,
              time: 134461,
              lyric: "而云朵太远太轻",
              pre: 138900,
            },
            {
              minute: 2,
              second: 18,
              millisecond: 900,
              time: 138900,
              lyric: "辗转之后各安天命",
              pre: 143062,
            },
            {
              minute: 2,
              second: 23,
              millisecond: 62,
              time: 143062,
              lyric: "",
              pre: 143401,
            },
            {
              minute: 2,
              second: 23,
              millisecond: 401,
              time: 143401,
              lyric: "我未入过繁华之境",
              pre: 147414,
            },
            {
              minute: 2,
              second: 27,
              millisecond: 414,
              time: 147414,
              lyric: "未听过喧嚣的声音",
              pre: 150945,
            },
            {
              minute: 2,
              second: 30,
              millisecond: 945,
              time: 150945,
              lyric: "未见过太多生灵",
              pre: 153163,
            },
            {
              minute: 2,
              second: 33,
              millisecond: 163,
              time: 153163,
              lyric: "未有过滚烫心情",
              pre: 155961,
            },
            {
              minute: 2,
              second: 35,
              millisecond: 961,
              time: 155961,
              lyric: "所以也未觉大洋正中",
              pre: 159650,
            },
            {
              minute: 2,
              second: 39,
              millisecond: 650,
              time: 159650,
              lyric: "有多么安静",
              pre: 163929,
            },
            {
              minute: 2,
              second: 43,
              millisecond: 929,
              time: 163929,
              lyric: "",
              pre: 164196,
            },
            {
              minute: 2,
              second: 44,
              millisecond: 196,
              time: 164196,
              lyric: "你的衣衫破旧",
              pre: 166196,
            },
            {
              minute: 2,
              second: 46,
              millisecond: 196,
              time: 166196,
              lyric: "而歌声却温柔",
              pre: 168345,
            },
            {
              minute: 2,
              second: 48,
              millisecond: 345,
              time: 168345,
              lyric: "陪我漫无目的地四处漂流",
              pre: 172429,
            },
            {
              minute: 2,
              second: 52,
              millisecond: 429,
              time: 172429,
              lyric: "我的背脊如荒丘",
              pre: 174529,
            },
            {
              minute: 2,
              second: 54,
              millisecond: 529,
              time: 174529,
              lyric: "而你却微笑摆首",
              pre: 177395,
            },
            {
              minute: 2,
              second: 57,
              millisecond: 395,
              time: 177395,
              lyric: "把它当成整个宇宙",
              pre: 181112,
            },
            {
              minute: 3,
              second: 1,
              millisecond: 112,
              time: 181112,
              lyric: "",
              pre: 181446,
            },
            {
              minute: 3,
              second: 1,
              millisecond: 446,
              time: 181446,
              lyric: "你与太阳挥手",
              pre: 183262,
            },
            {
              minute: 3,
              second: 3,
              millisecond: 262,
              time: 183262,
              lyric: "也同海鸥问候",
              pre: 185528,
            },
            {
              minute: 3,
              second: 5,
              millisecond: 528,
              time: 185528,
              lyric: "陪我爱天爱地的四处风流",
              pre: 189628,
            },
            {
              minute: 3,
              second: 9,
              millisecond: 628,
              time: 189628,
              lyric: "只是遗憾你终究",
              pre: 191696,
            },
            {
              minute: 3,
              second: 11,
              millisecond: 696,
              time: 191696,
              lyric: "无法躺在我胸口",
              pre: 194545,
            },
            {
              minute: 3,
              second: 14,
              millisecond: 545,
              time: 194545,
              lyric: "欣赏夜空最辽阔的不朽",
              pre: 198112,
            },
            {
              minute: 3,
              second: 18,
              millisecond: 112,
              time: 198112,
              lyric: "把星子放入眸",
              pre: 204328,
            },
            {
              minute: 3,
              second: 24,
              millisecond: 328,
              time: 204328,
              lyric: "",
              pre: 206642,
            },
            {
              minute: 3,
              second: 26,
              millisecond: 642,
              time: 206642,
              lyric: "你的指尖轻柔",
              pre: 209142,
            },
            {
              minute: 3,
              second: 29,
              millisecond: 142,
              time: 209142,
              lyric: "抚摸过我所有",
              pre: 211226,
            },
            {
              minute: 3,
              second: 31,
              millisecond: 226,
              time: 211226,
              lyric: "风浪冲撞出的丑陋疮口",
              pre: 215392,
            },
            {
              minute: 3,
              second: 35,
              millisecond: 392,
              time: 215392,
              lyric: "你眼中有春与秋",
              pre: 217609,
            },
            {
              minute: 3,
              second: 37,
              millisecond: 609,
              time: 217609,
              lyric: "胜过我见过爱过",
              pre: 220359,
            },
            {
              minute: 3,
              second: 40,
              millisecond: 359,
              time: 220359,
              lyric: "的一切山川与河流",
              pre: 223777,
            },
            {
              minute: 3,
              second: 43,
              millisecond: 777,
              time: 223777,
              lyric: "",
              pre: 224110,
            },
            {
              minute: 3,
              second: 44,
              millisecond: 110,
              time: 224110,
              lyric: "曾以为我肩头",
              pre: 226192,
            },
            {
              minute: 3,
              second: 46,
              millisecond: 192,
              time: 226192,
              lyric: "是那么的宽厚",
              pre: 228376,
            },
            {
              minute: 3,
              second: 48,
              millisecond: 376,
              time: 228376,
              lyric: "足够撑起海底那座琼楼",
              pre: 232376,
            },
            {
              minute: 3,
              second: 52,
              millisecond: 376,
              time: 232376,
              lyric: "而在你到来之后",
              pre: 234793,
            },
            {
              minute: 3,
              second: 54,
              millisecond: 793,
              time: 234793,
              lyric: "它显得如此清瘦",
              pre: 237527,
            },
            {
              minute: 3,
              second: 57,
              millisecond: 527,
              time: 237527,
              lyric: "我想给你能奔跑的岸头",
              pre: 245243,
            },
            {
              minute: 4,
              second: 5,
              millisecond: 243,
              time: 245243,
              lyric: "让你如同王后",
              pre: 249992,
            },
            {
              minute: 4,
              second: 9,
              millisecond: 992,
              time: 249992,
              lyric: "",
              pre: 251868,
            },
            {
              minute: 4,
              second: 11,
              millisecond: 868,
              time: 251868,
              lyric: " 配唱制作人 : 徐威",
              pre: 253744,
            },
            {
              minute: 4,
              second: 13,
              millisecond: 744,
              time: 253744,
              lyric: " 弦乐编写 : 欧阳箐宇",
              pre: 255620,
            },
            {
              minute: 4,
              second: 15,
              millisecond: 620,
              time: 255620,
              lyric: " 和声编唱 : 杨子轩",
              pre: 257496,
            },
            {
              minute: 4,
              second: 17,
              millisecond: 496,
              time: 257496,
              lyric: " 和声录制 : 杨子轩",
              pre: 259372,
            },
            {
              minute: 4,
              second: 19,
              millisecond: 372,
              time: 259372,
              lyric: " 人声录音室 : V-Studio",
              pre: 261248,
            },
            {
              minute: 4,
              second: 21,
              millisecond: 248,
              time: 261248,
              lyric: " 混音 : 周天澈",
              pre: 263124,
            },
            {
              minute: 4,
              second: 23,
              millisecond: 124,
              time: 263124,
              lyric: " 母带 : 周天澈",
              pre: 265000,
            },
            {
              minute: 4,
              second: 25,
              millisecond: 0,
              time: 265000,
              lyric: " 周深工作室出品",
              pre: null,
            },
            {
              minute: null,
              second: null,
              millisecond: null,
              time: null,
              lyric: "",
              pre: null,
            },
          ], //歌词
      currentTime: 0, //音乐当前进行时间
      countTime: 0, //总时长
      fm: localStorage.getItem("FM")
        ? JSON.parse(localStorage.getItem("FM"))
        : false, //是否为私人fm播放
    };
  },
  actions: {
    //修改播放列表
    changePlayList(item) {
      this.playList = item;
      localStorage.setItem("music", JSON.stringify(this.playList));
    },
    //修改播放状态
    changePlayReady(state) {
      if (arguments[0] !== undefined) {
        this.playReady = state;
      } else {
        this.playReady = !this.playReady;
      }
    },
    //修改弹窗状态
    changePlayPop(state) {
      if (arguments[0] !== undefined) {
        this.playPop = state;
      } else {
        this.playPop = !this.playPop;
      }
    },
    //修改歌词显示状态
    changeLyric() {
      this.lyric = !this.lyric;
    },
    //修改第几首歌
    changePlayIndex(type, num) {
      //type=0修改 type=1下一首 typ=-1上一首
      if (type == 0) {
        this.playIndx = num;
      } else if (type == 1) {
        if (this.playIndx == this.playList.length - 1) {
          this.playIndx = 0;
          this.getMusicLyric();
        } else {
          this.playIndx++;
          this.getMusicLyric();
        }
      } else if (type == -1) {
        if (this.playIndx == 0) {
          this.playIndx = this.playList.length - 1;
          this.getMusicLyric();
        } else {
          this.playIndx--;
          this.getMusicLyric();
        }
      }
      localStorage.setItem("musicIdx", this.playIndx);
    },
    //获取歌词
    async getMusicLyric() {
      let res = await getMusicLyric(this.playList[this.playIndx].id);
      this.musicLyric = [];
      res.data.lrc.lyric.split(/[\r\n]/g).forEach((v) => {
        this.musicLyric.push({
          minute: parseInt(v.slice(1, 3)),
          second: parseInt(v.slice(4, 6)),
          millisecond: parseInt(v.slice(7, 10)),
          time:
            parseInt(v.slice(1, 3)) * 60 * 1000 +
            parseInt(v.slice(4, 6)) * 1000 +
            parseInt(v.slice(7, 10)),
          lyric:
            v.slice(10, 11) === "]"
              ? v.slice(11, v.length)
              : v.slice(10, v.length),
        });
      });
      this.musicLyric.forEach((item, i) => {
        if (
          i === this.musicLyric.length - 1 ||
          isNaN(this.musicLyric[i + 1].time)
        ) {
          item.pre = this.countTime;
        } else {
          item.pre = this.musicLyric[i + 1].time;
        }
      });
      localStorage.setItem("lyric", JSON.stringify(this.musicLyric));
    },
    //更新当前音乐进行时间
    changeCurrentTime(currtime, duration) {
      this.currentTime = currtime;
      if (arguments[1] !== undefined) {
        if (this.countTime != duration) this.countTime = duration;
        if (this.countTime == this.currentTime) {
          if (this.playList.length - 1 === this.playIndx) {
            this.playIndx = 0;
            this.getMusicLyric();
          } else {
            this.playIndx++;
            this.getMusicLyric();
          }
        }
      }
    },
    //更新FM状态
    changeFM(state) {
      if (arguments[0] !== undefined) {
        this.fm = state;
      } else {
        this.fm = !this.fm;
      }
      localStorage.setItem("FM", JSON.stringify(this.fm));
    },
  },
});
